//##include('../block/lazyload.js');
//##include('../block/doT.js');
//##include('../block/iscroll-probe.js');

var $doc,
    $win,
    docH,
    avaH,
    $mainContainer,
    $contentWrapper,
    $contentWrapperH,
    $favlist,
    _threshold = 0,
    $activeData,
    myScroll,
    position,
    eleTopArr = [],
    eleDateArr = [];

var _view = {
    init: function() {
        setTimeout(function(){
            _view.initEleTop();
            _view.initEleDate();

            _view.containerScrollLoaded();
            _view.documentMovetouchDefault();
            _view.lazyimg();
        },50);

    },

    initEleDate : function(){
        $favlist.find('li').each(function(){
            eleDateArr.push($(this).attr('data-time'));
        });
    },

    initEleTop : function(){

        docH = $doc.height();				//页面高度
        //avaH = document.documentElement.clientHeight;	//可见区域高度

        $favlist.find('li').each(function(){
            var tmpT = parseInt($(this).offset().top);
            eleTopArr.push(tmpT);
        });
    },

    containerScrollLoaded : function(){

        var $a = binarySearch(eleTopArr,0,0,eleTopArr.length);
        var $ward= ward($a);
        $activeData.find('.data-time').html($ward);

        myScroll = new IScroll(".content-wrapper", {
            probeType: 3,
            useTransform : true,
            mouseWheel: false,
            vScrollbar: false,
            hScrollbar : false,
            useTransition : true,
            momentum : false,
            bounce : false,
            shrinkScrollbars : 'clip',
            preventDefault : false,

            //HWCompositing : true,
            fadeScrollbars : false
            //snap : 'li'
        });

        //myScroll.on('scroll', throttle(this.iScrollCtrl,5,20));
        //myScroll.on('scrollEnd', throttle(this.iScrollCtrl,5,20));

        myScroll.on('scroll', this.iScrollCtrl);
        myScroll.on('scrollEnd', this.iScrollCtrl);

    },

    documentMovetouchDefault : function(){
        document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
    },

    iScrollCtrl : function(){

        //console.log($contentWrapper[0].clientHeight);
        $contentWrapperH = $contentWrapper[0].clientHeight;
        //var scaleY = parseInt((this.y/$('.content-item')[0].scrollHeight)*avaH);
        //var scaleY = parseInt((this.y/Math.abs(this.maxScrollY))*(avaH-50));
        var scaleY = parseInt((this.y/Math.abs(this.maxScrollY))*($contentWrapperH-50));

        $activeData.css({'transform':'translate3d(0px,'+ -(scaleY) +'px, 0px) scale(1)'});
        //$activeData.css({'top':-scaleY});
        var $a = binarySearch(eleTopArr,Math.abs(scaleY+this.y),0,eleTopArr.length);
        var $ward= ward($a);

        $activeData.find('.data-time').html($ward);
        $(".lazy").lazyload({
            effect : "show",
            threshold : 200
        });

    },


    lazyimg:function(){
        $(".lazy").lazyload({
            effect : "show",
            threshold : 200
        });
    }
};


function throttle(fn, delay, mustRunDelay){
    var timer = null;
    var t_start;
    return function(){
        var context = this, args = arguments, t_curr = +new Date();
        clearTimeout(timer);
        if(!t_start){
            t_start = t_curr;
        }
        if(t_curr - t_start >= mustRunDelay){
            fn.apply(context, args);
            t_start = t_curr;
        }
        else {
            timer = setTimeout(function(){
                fn.apply(context, args);
            }, delay);
        }
    };
}


function binarySearch(arr,findVal,leftIndex,rightIndex){

    var find;
    if(leftIndex > rightIndex){
        find = leftIndex-1;
        return find;
    }
    var midIndex = Math.floor((leftIndex+rightIndex)/2);
    var midVal = arr[midIndex];
    if(midVal>findVal){

        return binarySearch(arr,findVal,leftIndex,midIndex-1);

    }else if(midVal<findVal){

        return binarySearch(arr,findVal,midIndex+1,rightIndex);

    }else {
        //var find = midIndex +1;
        if(midIndex == 0){
            find = midIndex;
        }else{
            find = midIndex - 1;
        }

        return find;
    }
}
function ward(a){
    if(a<0){
        return '';
    }
    if(a>eleTopArr.length){
        a=eleTopArr.length;
    }

    if(a == 0){
        a=0;
    }

    return eleDateArr[a];
}


function init() {
    _view.init();
}



$(document).ready(function(){
    $doc = $(document);
    $win = $(window);
    $mainContainer = $("#main-container");
    $favlist = $('#fav-list');
    $activeData = $('.active-data');
    $contentWrapper = $('.content-wrapper');
    avaH = document.documentElement.clientHeight;

    init();

    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

});
